package com.shiro.demo.project.system.login.controllers;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 验证码控制器
 * 功能：
 *     1、提供验证码图片
 *     2、提供Ajax验证服务
 */
@Controller
@RequestMapping("/captcha")
public class CaptchaController {
    @Resource(name = "defaultKaptchaProducer")
    private Producer defaultKaptchaProducer;

    @Resource(name = "mathKaptchaProducer")
    private Producer mathKaptchaProducer;

    @Getter
    @Setter
    private String type;

    protected void writeImage(HttpServletRequest request, HttpServletResponse response)
    {
        ServletOutputStream out = null;
        try
        {
            HttpSession session = request.getSession();
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
            response.setHeader("Pragma", "no-cache");
            response.setContentType("image/jpeg");

            String type = (String)request.getParameter("type");
            String capStr = null;
            String code = null;
            BufferedImage bi = null;
            if ("math".equals(type))
            {
                String capText = mathKaptchaProducer.createText();
                capStr = capText.substring(0, capText.lastIndexOf("@"));
                code = capText.substring(capText.lastIndexOf("@") + 1);
                bi = mathKaptchaProducer.createImage(capStr);
            }
            else
            {
                capStr = code = defaultKaptchaProducer.createText();
                bi = defaultKaptchaProducer.createImage(capStr);
            }
            session.setAttribute(Constants.KAPTCHA_SESSION_KEY, code);
            out = response.getOutputStream();
            ImageIO.write(bi, "jpg", out);
            out.flush();

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (out != null)
                {
                    out.close();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    @GetMapping
    public void captchaImage(HttpServletRequest request, HttpServletResponse response){
        writeImage(request, response);
    }

    @PostMapping
    @ResponseBody
    public boolean verifyCaptcha(@NonNull @RequestParam("code") String code){
        Session session = SecurityUtils.getSubject().getSession();
        String answer = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        return code.equalsIgnoreCase(answer);
    }
}
